﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace WPFExcelReport
{
    public partial class TSAV37
    {

        public TSAV37()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_MKRF();
            DataContext = MV;
            InitializeComponent();
            TMV.SetUpAV37();
            NameScope.SetNameScope(this, pagescope);
        }
        public override void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                if (DesignerProperties.GetIsInDesignMode(this))
                    return;
                if (IsLoaded)
                    return;
                base.Window_Loaded(sender, e);
                this.Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new LOADBYAUFDelegate(SetupMachine)); 
                //NormalTask = System.Threading.Tasks.Task.Factory.StartNew(() => SetupMachine(), new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void SetupMachine()
        {
            
            DENSITY = 24;
            STKMachineTable = this.MachineTable;
            STKmachines = this.machines;
            STKZoneCalendar = this.ZoneCalendar;
            base.SetupMachine();
            DENSITY = 1;
            DENSITY = 24;

        }


        public override void OnShowWJobChg(bool show)
        {
            if (!show)
                return;
            ShowAllMachine = false;
            foreach (Control child in MachineTable.Children)
            {
                UIAM m = (UIAM)child;
                if (m == null) continue;
                if (m.JOBS == null)
                {
                    m.IsShow = false;
                    continue;
                }
                if (m.JOBS.Rows.Count > 0)
                {
                    m.IsShow = true;
                }
                else
                {
                    m.IsShow = false;
                }
            }
        }

        public override void OnShowAllChg(bool show)
        {
            if (!show)
                return;
            ShowWJobs = false;
            foreach (Control child in MachineTable.Children)
            {
                UIAM m = (UIAM)child;
                if (m == null) continue;
                m.IsShow = true;
            }
        }

        public void Current_Click(object sender, RoutedEventArgs e)
        {
            int current = Scheduler.MCALENDAR.CurrentBlock;
            double x = current * Scheduler.MCALENDAR.PIXELS;
            ScrollToPosition(mainViewer, x, 0);

        }
        public override void OnJOBIDChg(string value)
        {
            if (value.Length == 0)
                return;
            FindJob(value);

        }
        public void FindMachine(string _m)
        {
            UIAM machine = (UIAM)this.FindName(_m);
            if (machine != null)
            {
                FocusMachine(mainViewer, MachineTable, machine);
            }
        }
        public override void OnShowGanttChg(bool show)
        {
            if (!IsShowGantt)
            {
                LastShow = false;
                return;
            }
            if (LastShow)
                return;
            LastShow = true;
            Scheduler.SetProd.Clear();
            Scheduler.SetProd.Merge(MV.SetProd);
            RemoveAllJobs();
            Scheduler.PrepareAllJob(false);
            Scheduler.ShowMachine();
            ShowWJobs = false;
            ShowWJobs = true;

        }
        public void FindJob(string _JOBID)
        {
            try
            {
                string jobid = "Job" + _JOBID;// KRS + "_" + LFD + "_" + POS + "_" + dwJob["MKFTP"].ToString();
                UIJOB job = (UIJOB)this.FindName(jobid);
                if (job == null)
                {
                    return;
                }
                if (job.MGANTT == null)
                {
                    return;
                }
                string SMTYP = job.MGANTT.Machine.SMTYP;
                if (!IsMachineType)
                {
                    SMTYP = "";
                }
                //      Scheduler.FilterByMachineType(SMTYP);
                if (Lastjob != null)
                {
                    UnFocusJob(Lastjob);
                }
                FocusJob(mainViewer, machines, job);
                return;
                string filter = "ABFAUFKRS='" + job.dwJob["ABFAUFKRS"].ToString()
                    + "' AND ABFAUFLFD='" + job.dwJob["ABFAUFLFD"].ToString() + "' AND ABFAUFPOS='"
                    + job.dwJob["ABFAUFPOS"].ToString() + "'";

                DataRow[] rows = TBMain.Select(filter);
                int count = rows.Length;
                foreach (DataRow dwJob in rows)
                {
                    jobid = "Job" + dwJob["ABAID"].ToString();// KRS + "_" + LFD + "_" + POS + "_" + dwJob["MKFTP"].ToString();
                    job = (UIJOB)this.FindName(jobid);
                    if (job == null)
                    {
                        return;
                    }
                    FocusJob(mainViewer, machines, job);
                    break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void mainViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            vwCalendar.ScrollToHorizontalOffset(mainViewer.HorizontalOffset);
            vwMachine.ScrollToVerticalOffset(mainViewer.VerticalOffset);
        }


    }
}
